"""
@author: chen
@file: 猜拳游戏.py
@time: 2023/4/14 17:14
"""

# 猜拳游戏
# 用户 -> 创建房间、加入房间
# 加入房间之后，人数等于两个人就可以开始游戏
# 两个人互相猜拳，按任意键A，B两个人随机出剪刀石头布，输的人扣100金豆
# 直到金豆输完，或按q键退出

import random

# 房间类  ->  房间id、房间里面的人[]
# 方法  ->  进入房间、退出房间

class Room:
    def __init__(self, room_id):
        self.room_id = room_id
        self.user = []
        self.user_num = 0

    def check_user(self):
        if self.user_num == 0:
            print("空房间".center(50, "*"))
            return True
        elif self.user_num == 1:
            print("房间人数为1，还差一人开始游戏".center(40, "*"))
            return True
        elif self.user_num == 2:
            print(f"{self.room_id}房间人数已满，可以开始游戏".center(40, "*"))
            return False

    def into_user(self, user_num):
        self.user_num += 1
        self.user.append(user_num.user_id)

    def start_game(self, user_id1, user_id2):
        if user_id1.init_amount <= 0:
            print(f"{user_id1.user_id}的金豆数不足，请充值")
            return 0
        if user_id2.init_amount <= 0:
            print(f"{user_id2.user_id}的金豆数不足，请充值")
            return 0
        x = random.randint(0, 2)
        y = random.randint(0, 2)
        print(f"{user_id1.user_id}出{hand[x]}, {user_id2.user_id}出{hand[y]}".center(40, "."))
        if x == y:
            print("平局!".center(40, "."))
        elif (x == 0 and y == 1) or (x == 1 and y == 2) or (x == 2 and y == 0):
            user_id2.init_amount += 100
            user_id1.init_amount -= 100
            print(f"{user_id2.user_id}赢！+100金豆。{user_id1.user_id}输，-100金豆".center(40, "."))
        else:
            user_id2.init_amount -= 100
            user_id1.init_amount += 100
            print(f"{user_id1.user_id}赢！+100金豆。{user_id2.user_id}输，-100金豆".center(40, "."))
        print(f"{user_id1.user_id}余额：{user_id1.init_amount},{user_id2.user_id}余额：{user_id2.init_amount}".center(40, "."))
        return 1



# 用户类  ->  用户id、金豆数量、属于哪个房间
# 方法  ->  猜拳

class User:
    def __init__(self, user_id, init_amount):
        self.user_id = user_id
        self.init_amount = init_amount
        self.room_id = 0

    def into_room(self, room_id):
        if room_id.user_num == 2:
            print("房间已满员，请进入别的房间".center(40, "."))
            return
        self.room_id = room_id
        room_id.into_user(self)

    def out_room(self):
        roomid = self.room_id
        for k, v in enumerate(room):
            if v == use.room_id:
                roomid = room_R[k]
        roomid.user_num -= 1
        # roomid.user.remove(self.user_id)
        self.room_id = 0
        print("退出房间成功！".center(40, "."))


    # def register(self, name, amount):
    #     if name not in user_id:
    #         #创建一个用户
    #         user_id[name] = amount
    #         file_name = self


room = []
room_R = []
hand = ["石头", "布", "剪刀"]
users = {}

current_room = {}
start_room = None

while 1:
    u = input("0.用户登录\n1.开始游戏\n2.退出房间\n请输入你的选择（按q退出）：")
    if u == "q":
        print("退出游戏".center(40, "."))
        break
    elif u == "0":
        print("欢迎进入猜拳游戏！".center(40, "."))
        id = input("请输入用户名登录：")
        user = User(id, 200)
        users[id] = user
        print(f"{id}登录成功！你的金豆为500".center(40, "."))
        print(f"当前有房间：{room}".center(40, "."))
        room_id = int(input("你要进入哪个房间呢："))
        if room_id not in room:
            # 创建房间，房间号写入room，房间实例写入room_R
            room.append(room_id)
            room_id = Room(room_id)
            room_R.append(room_id)
            # 进入房间
            user.into_room(room_id)
            print("没有此房间，已创建并进入".center(40, "."))
        else:
            for k, v in enumerate(room):
                if v == room_id:
                    room_id = room_R[k]
                    user.into_room(room_id)
                    break
        room_id.check_user()
        # flag = room_id.check_user()
        # if flag == False:
        #     start_room = room_id
        current_room[room_id.room_id] = room_id
    elif u == "1":
        for k, v in enumerate(room):
            print(f"房间：{v}，人数:{room_R[k].user_num}人".center(40, "."))
        r = int(input("请输入要开始游戏的房间号："))
        if r not in room:
            print("没有该房间".center(40, "."))
        start_room = current_room[r]
        if start_room.user_num < 2:
            print("该房间未满员".center(40, "."))
            continue
        flag = 1
        while flag:
            c = input("按任意键开始游戏（按q退出）：")
            if c == "q":
                print("结束游戏".center(40, "."))
                break
            else:
                flag = start_room.start_game(start_room.user[0], start_room.user[1])
    elif u == "2":
        use = input("请输入要退出房间的用户：")
        use = users[use]
        use.out_room()




